<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="AVFoundation的文本朗诵主要由以下三个类组成：AVSpeechSynthesizer（语音合成器）、AVSpeechUtterance（语音内容）和AVSpeechSynthesisVoice（语音口音）。这三个类的关系如下图：简单来说就是一个语音合成器（AVSpeechSynthesiz">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="AVFoundation学习笔记二  Speech Synthesis">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="AVFoundation的文本朗诵主要由以下三个类组成：AVSpeechSynthesizer（语音合成器）、AVSpeechUtterance（语音内容）和AVSpeechSynthesisVoice（语音口音）。这三个类的关系如下图：简单来说就是一个语音合成器（AVSpeechSynthesiz">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>AVFoundation学习笔记二  Speech Synthesis - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>AVFoundation学习笔记二  Speech Synthesis</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2018-10-24
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        


<a href="/blog/tags/文本语音播报/">#文本语音播报</a>


                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                        

<a href="/blog/categories/AVFoundation/">AVFoundation</a>

                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>AVFoundation的文本朗诵主要由以下三个类组成：AVSpeechSynthesizer（语音合成器）、AVSpeechUtterance（语音内容）和AVSpeechSynthesisVoice（语音口音）。这三个类的关系如下图：<br><img src="http://cyrus_huang.gitee.io/blog/2018/10/24/AVFoundation学习笔记一-AVSpeechSynthesizer/SpeechSynthesizer.png" alt=""><br>简单来说就是一个语音合成器（AVSpeechSynthesizer）可以播放一个或多个可以设置口音（AVSpeechSynthesisVoice）、语调、语速的语音内容（AVSpeechUtterance）。</p>
<p>具体代码步骤：<br>1、初始化一个AVSpeechSynthesizer对象<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">_synthesizer = [[AVSpeechSynthesizer alloc] init];</span><br></pre></td></tr></table></figure></p>
<p>2、创建要朗诵的内容<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">_speechStrings = @[@&quot;Hello AV Foundation. How are you?&quot;,</span><br><span class="line">                           @&quot;I&apos;m well! Thanks for asking.&quot;,</span><br><span class="line">                           @&quot;Are you excited about the book?&quot;,</span><br><span class="line">                           @&quot;Very! I have always felt so misunderstood.&quot;,</span><br><span class="line">                           @&quot;What&apos;s your favorite feature?&quot;,</span><br><span class="line">                           @&quot;Oh, they&apos;re all my babies.  I couldn&apos;t possibly choose.&quot;,</span><br><span class="line">                           @&quot;It was great to speak with you!&quot;,</span><br><span class="line">                           @&quot;The pleasure was all mine!  Have fun!&quot;</span><br><span class="line">                           ];</span><br></pre></td></tr></table></figure></p>
<p>3、为每个字符串创建一个AVSpeechUtterance对象,并传递给self.synthesizer进行播放<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">for (NSUInteger i = 0; i &lt; self.speechStrings.count; i++) &#123;</span><br><span class="line">        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.speechStrings[i]];</span><br><span class="line">        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@&quot;en-US&quot;];</span><br><span class="line">        utterance.rate = 0.4;</span><br><span class="line">        utterance.pitchMultiplier = 0.8f;       //声调，0.5（低音调）-2.0（高音调）</span><br><span class="line">        utterance.postUtteranceDelay = 0.1f;    //播放下一句之前有短时间的暂停</span><br><span class="line">        [self.synthesizer speakUtterance:utterance];</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p>相关属性：</p>
<p>AVSpeechSynthesisVoice相关属性</p>
<p>（1）language:voice的主要属性，可以根据language创建voice。可以使用<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">+ (NSArray&lt;AVSpeechSynthesisVoice *&gt; *)speechVoices;</span><br></pre></td></tr></table></figure></p>
<p>获取所有支持的language,具体如下：</p>
<ul>
<li>Arabic (ar-SA)</li>
<li>Chinese (zh-CN, zh-HK, zh-TW)</li>
<li>Czech (cs-CZ)</li>
<li>Danish (da-DK)</li>
<li>Dutch (nl-BE, nl-NL)</li>
<li>English (en-AU, en-GB, en-IE, en-US, en-ZA)</li>
<li>Finnish (fi-FI)</li>
<li>French (fr-CA, fr-FR)</li>
<li>German (de-DE)</li>
<li>Greek (el-GR)</li>
<li>Hebrew (he-IL)</li>
<li>Hindi (hi-IN)</li>
<li>Hungarian (hu-HU)</li>
<li>Indonesian (id-ID)</li>
<li>Italian (it-IT)</li>
<li>Japanese (ja-JP)</li>
<li>Korean (ko-KR)</li>
<li>Norwegian (no-NO)</li>
<li>Polish (pl-PL)</li>
<li>Portuguese (pt-BR, pt-PT)</li>
<li>Romanian (ro-RO)</li>
<li>Russian (ru-RU)</li>
<li>Slovak (sk-SK)</li>
<li>Spanish (es-ES, es-MX)</li>
<li>Swedish (sv-SE)</li>
<li>Thai (th-TH)</li>
<li>Turkish (tr-TR)</li>
</ul>
<p>（2）<strong><em>quality</em></strong>：声音质量，枚举值<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">typedef NS_ENUM(NSInteger, AVSpeechSynthesisVoiceQuality) &#123;</span><br><span class="line">    AVSpeechSynthesisVoiceQualityDefault = 1,	//默认</span><br><span class="line">    AVSpeechSynthesisVoiceQualityEnhanced		//增强</span><br><span class="line">&#125; NS_ENUM_AVAILABLE(10_14, 9_0);</span><br></pre></td></tr></table></figure></p>
<p>AVSpeechUtterance相关属性</p>
<p>（1）<strong><em>rate</em></strong>:指定播放语音时的速率(0.0-1.0),即AVSpeechUtteranceMinimumSpeechRate和AVSpeechUtteranceMaximumSpeechRate之间</p>
<p>（2）<strong><em>pitchMultiplier</em></strong>：设置声调,属性值介于0.5(低音调)~2.0(高音调)之间</p>
<p>（3）<strong><em>volume</em></strong>:设置音量(0.0-1.0)</p>
<p>（4）<strong><em>preUtteranceDelay</em></strong>：postUtteranceDelay告诉synthesizer本句朗读结束后要延迟多少秒再接着朗读下一秒,对应的属性还有<strong><em>preUtteranceDelay</em></strong></p>
<p>此外,<font color="0xff000000"><strong><em>AVSpeechSynthesizerDelegate</em></strong></font>中还提供了一些监听朗读状态的方法.</p>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>